//
//  XHUImageViewController.m
//  testApp
//
//  Created by ColdMountain on 2017/3/4.
//  Copyright © 2017年 ColdMountain. All rights reserved.
//

#import "XHUImageViewController.h"

#import "XHImageViewCell.h"

#import "Services.h"

#import <SDWebImage/UIImageView+WebCache.h>

@interface XHUImageViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray * m_array;
@property (assign, nonatomic) CGFloat height;
@end

@implementation XHUImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHImageViewCell" bundle:nil] forCellReuseIdentifier:XHImageViewCellIdentifier];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self banner];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)banner
{
    [ServiceManager postBannerWithSuccess:^(NSDictionary *data) {
        self.m_array=(NSArray*)data;
        [self.ui_tableView reloadData];
    } failure:^(NSDictionary *data) {
        
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = nil;
    cell = [self imageViewCell:indexPath];
    return cell;
}

- (XHImageViewCell *)imageViewCell:(NSIndexPath*)indexPath
{
    XHImageViewCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHImageViewCellIdentifier];
    NSDictionary * dic = self.m_array[indexPath.row];
    [cell.ui_imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"imageurl"]]];
    UIImage *image=nil;
    NSURL *url=[NSURL URLWithString:dic[@"imageurl"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    image = [UIImage imageWithData:data];
    NSLog(@"%.2f",image.size.height);
    NSLog(@"%.2f",image.size.width);
    CGFloat xishu;
    xishu=image.size.width/image.size.height;
    self.height=xishu;
    NSLog(@"%.2f",xishu);
    return cell;
}


#pragma - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=0;
    if (self.height>0)
    {
        height=SCREEN_WIDTH/self.height;
    }
    
    return height;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
